package NullObject.After;

/**
 * Now check out our solution to make sure we don't have to check for null.
 * We now have a shield. Now we have a
 */
public class Game
{

}

class Player
{
    int lifeLeft = 100;
    Shield shield = new NullShield();

    void playerAttacked( int damage )
    {
        int excessDamage = 0;       // This is damage beyond what the shield can take.
        excessDamage = shield.takeDamage( damage );
        
        takeDamage( excessDamage );
    }

    void takeDamage( int damage )
    {
        lifeLeft = lifeLeft - damage;
        if ( lifeLeft < 0 )         // You are dead.
            lifeLeft = 0;
    }
}

class Shield
{
    int shieldHitPoints;

    Shield ( int sheildHitPoints )
    {
        this.shieldHitPoints = shieldHitPoints;
    }

    public int takeDamage( int damage )
    {
        shieldHitPoints = shieldHitPoints - damage;
        if( shieldHitPoints < 0 )
        {
            shieldHitPoints = 0;    // Make our shield worth nothing.
            return shieldHitPoints; // return the damage that goes beyond what the shield can handle.
        }
        return 0;                   // The shield could hold all the damage.
    }
}

class NullShield extends Shield
{
    NullShield()
    {
        super( 0 );
    }

    public int takeDamage( int damage )
    {
        return damage;              // Return all the damage since we don't have a real shield right now.     
    }
}

